package com.interview.mapper;

import com.interview.entity.QuesUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author JiangYingbo
 */
@Repository
public interface QuesUserMapper {

    /**
     * 根据条件查询问题用户表（quesDesc改为模糊查询）
     *
     * @param condition 查询条件
     * @return 问题用户表列表
     */
    @Select({"<script>" +
            "select q.ques_user_id, q.user_id, q.ques_desc, ",
            "q.ex_reply, q.user_reply, q.ques_status, q.point, q.create_time,q.suggestion ",
            "from interview_ai.ques_user q ",
            "<where>",
            "<if test='quesUserId != null'>and q.ques_user_id = #{quesUserId}</if>",
            "<if test='userId != null'>and q.user_id = #{userId}</if>",
            "<if test='quesDesc != null'>",
            "and q.ques_desc LIKE CONCAT('%', #{quesDesc}, '%')",
            "</if>",
            "<if test='exReply != null'>and q.ex_reply = #{exReply}</if>",
            "<if test='userReply != null'>and q.user_reply = #{userReply}</if>",
            "<if test='quesStatus != null'>and q.ques_status = #{quesStatus}</if>",
            "<if test='point != null'>and q.point = #{point}</if>",
            "<if test='createTime != null'>and q.create_time = #{createTime}</if>",
            "<if test='suggestion != null'>and q.suggestion = #{suggestion}</if>",
            "</where>",
            "order by q.ques_status" + "</script>"})
    List<QuesUser> select(QuesUser condition);

    /**
     * 插入一条用户答题记录
     *
     * @param quesUser 插入字段
     * @return 影响条目数
     */
    @Insert("insert into interview_ai.ques_user ( " +
            "user_id, ques_desc, ex_reply )" +
            "values ( " +
            "#{userId}, #{quesDesc}, #{exReply}) ")
    @Options(useGeneratedKeys = true, keyProperty = "quesUserId")
    int insert(QuesUser quesUser);

    /**
     * 删除用户答题记录
     *
     * @param quesUserId 用户id
     * @return 影响条目数
     */
    @Delete("delete from interview_ai.ques_user where ques_user_id = #{quesUserId}")
    int delete(Integer quesUserId);

    @Update("update ques_user set " +
            "ques_user_id =#{quesUserId}, " +
            "user_id=#{userId}, " +
            "ques_desc=#{quesDesc}, " +
            "ex_reply=#{exReply}, " +
            "user_reply=#{userReply}, " +
            "suggestion=#{suggestion}, " +
            "ques_status=#{quesStatus}, " +
            "point=#{point} " +
            "where ques_user_id=#{quesUserId}")
    int updateUserReply(QuesUser quesUser);
}
